/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.catalog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.services.manage.catalog.CatalogService;
import shop.services.manage.catalog.bean.Catalog;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**   
 * @类名称：CatalogAction      
 * @创建人：Ltz   
 * @创建时间：2017-09-20 下午15:34:37      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/catalog/")
public class CatalogAction extends BaseController<Catalog> {
    private static final Logger logger = LoggerFactory.getLogger(CatalogAction.class);
    @Autowired
    private CatalogService catalogService;
    private static final String page_toList = "/manage/catalog/catalogList";
    public CatalogService getService() {
        return catalogService;
    }

    private CatalogAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     * 删除单个信息
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "deleteByIdJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteByIdJson(@RequestParam(value = "id") int id) throws Exception {
        JSONResult jsonResult = new JSONResult();
        getService().deleteById(id);
        jsonResult.setMessage("success");
        return jsonResult;
    }

    /**
     * 查找所有一级目录
     * @return
     */
    @RequestMapping(value = "getOneLevelCatalog",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getOneLevelCatalog(HttpServletRequest request, @ModelAttribute("e") Catalog e) throws Exception {
        JSONResult jsonResult = new JSONResult();
        List<Catalog> rsList = getService().selectOneLevel(e);
        jsonResult.setData(rsList);
        return jsonResult;
    }

    /**
     * 查询二级目录
     * @return
     */
    @RequestMapping(value = "getTwoLevelCatalog",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getTwoLevelCatalog(String pid) throws Exception {
        List<Catalog> rsList = catalogService.selectTwoLevel(pid);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(rsList);
        return jsonResult;
    }

    /**
     * 查找所有一级目录
     * @return
     */
    @RequestMapping(value = "getParentCatalog",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getParentCatalog(){
        JSONResult rs=new JSONResult();
        Catalog catalog=new Catalog();
        catalog.setPid("0");
        List<Catalog> catalogList=getService().selectList(catalog);
        rs.setData(catalogList);
        rs.setSuccess(true);
        return rs;
    }

    /**
     * 查询二级目录
     * @param pid 一级目录id
     */
    @RequestMapping(value = "getCatalog",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getCatalog(String pid){
        JSONResult rs =new JSONResult();
        Catalog catalog=new Catalog();
        catalog.setPid(pid);
        List<Catalog> catalogList=getService().selectByPid(catalog);
        rs.setData(catalogList);
        rs.setSuccess(true);
        return  rs;
    }
}
